﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.Templet;
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormSelectOrders : FormTemplet
    {

        private int _nCustomerID = -1;

        private int _nProjectID = -1;

        private static OrdersAmountData s_OrdersAmountInfo = null;


        private FormSelectOrders()
        {
            this.InitializeComponent();
        }

        public FormSelectOrders(int nCustomerID, string sCustomerName) : this()
        {
            this._nCustomerID = nCustomerID;
            this.txtCustomerName.Text = sCustomerName;
            FormSelectOrders.s_OrdersAmountInfo = null;
        }

        private void FormQueryOrders_Load(object sender, EventArgs e)
        {
            this.dtpSpeakForDateStart.Enabled = false;
            this.dtpSpeakForDateEnd.Enabled = false;
            this.dtpDeliveryDateStart.Enabled = false;
            this.dtpDeliveryDateEnd.Enabled = false;
            this.ckLailiao.Enabled = false;
            this.ckQuliao.Enabled = false;
            this.chkLockedOrderData.Enabled = false;
            this.chkCancelledOrder.Enabled = false;
            this.btnSelectCustomer.Enabled = false;
        }

        private void btnSelectCustomer_Click(object sender, EventArgs e)
        {
        }

        private void btnSelectProject_Click(object sender, EventArgs e)
        {
            ProjectData projectData = FormSelectProject.ShowBox(this._nCustomerID);
            if (projectData != null)
            {
                this._nProjectID = projectData.ProjectID;
                this.txtProjectName.Text = projectData.ProjectName + "(" + projectData.Linkman + ")";
            }
        }

        private void chkSpeakForDateUsed_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkSpeakForDateUsed.Checked)
            {
                this.dtpSpeakForDateStart.Enabled = true;
                this.dtpSpeakForDateEnd.Enabled = true;
            }
            else
            {
                this.dtpSpeakForDateStart.Enabled = false;
                this.dtpSpeakForDateEnd.Enabled = false;
            }
        }

        private void chkDeliveryDateUsed_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkDeliveryDateUsed.Checked)
            {
                this.dtpDeliveryDateStart.Enabled = true;
                this.dtpDeliveryDateEnd.Enabled = true;
            }
            else
            {
                this.dtpDeliveryDateStart.Enabled = false;
                this.dtpDeliveryDateEnd.Enabled = false;
            }
        }

        private void chkLockedOrderDataUsed_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkLockedOrderDataUsed.Checked)
            {
                this.chkLockedOrderData.Enabled = true;
            }
            else
            {
                this.chkLockedOrderData.Enabled = false;
            }
        }

        private void chkCancelledOrderUsed_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkCancelledOrderUsed.Checked)
            {
                this.chkCancelledOrder.Enabled = true;
            }
            else
            {
                this.chkCancelledOrder.Enabled = false;
            }
        }

        private void ckLailiaoUsed_CheckedChanged(object sender, EventArgs e)
        {
            if (this.ckLailiaoUsed.Checked)
            {
                this.ckLailiao.Enabled = true;
            }
            else
            {
                this.ckLailiao.Enabled = false;
            }
        }

        private void ckQuliaoUsed_CheckedChanged(object sender, EventArgs e)
        {
            if (this.ckQuliaoUsed.Checked)
            {
                this.ckQuliao.Enabled = true;
            }
            else
            {
                this.ckQuliao.Enabled = false;
            }
        }

        private void txtSalesperson_DoubleClick(object sender, EventArgs e)
        {
            string text = FormSelectSalesperson.ShowBox("请选择业务员...");
            if (text != null)
            {
                this.txtSalesperson.Text = text.Trim();
            }
        }

        private OrderCondition GetConditionForQuery()
        {
            OrderCondition orderCondition = new OrderCondition();
            OrderCondition result;
            if (this.txtCustomerName.Text.Trim() == "")
            {
                MessageBox.Show("客户名称必须选择！！！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                result = null;
            }
            else
            {
                if (this.txtCustomerName.Text.Trim() != "")
                {
                    if (this.txtCustomerName.Text.Trim() == "" || this._nCustomerID == -1)
                    {
                        MessageBox.Show("请选择客户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtCustomerName.Focus();
                        result = null;
                        return result;
                    }
                    orderCondition.CustomerID = this._nCustomerID;
                }
                if (this.txtProjectName.Text.Trim() != "")
                {
                    if (this.txtProjectName.Text.Trim() == "" || this._nProjectID == -1)
                    {
                        MessageBox.Show("请选择计划", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtProjectName.Focus();
                        result = null;
                        return result;
                    }
                    orderCondition.ProjectID = this._nProjectID;
                }
                if (this.chkSpeakForDateUsed.Checked)
                {
                    int num = DateTime.Parse(this.dtpSpeakForDateStart.Value.ToShortDateString() + " 00:00:00").CompareTo(DateTime.Parse(this.dtpSpeakForDateEnd.Value.ToShortDateString() + " 23:59:59"));
                    if (num > 0)
                    {
                        MessageBox.Show("订购日期：后面的日期要大于或等于前面的日期", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.dtpSpeakForDateEnd.Focus();
                        result = null;
                        return result;
                    }
                    orderCondition.SpeakForDateStart = this.dtpSpeakForDateStart.Value.ToShortDateString() + " 00:00:00";
                    orderCondition.SpeakForDateEnd = this.dtpSpeakForDateEnd.Value.ToShortDateString() + " 23:59:59";
                }
                if (this.chkDeliveryDateUsed.Checked)
                {
                    int num = DateTime.Parse(this.dtpDeliveryDateStart.Value.ToShortDateString() + " 00:00:00").CompareTo(DateTime.Parse(this.dtpDeliveryDateEnd.Value.ToShortDateString() + " 23:59:59"));
                    if (num > 0)
                    {
                        MessageBox.Show("交货日期：后面的日期要大于或等于前面的日期", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.dtpSpeakForDateEnd.Focus();
                        result = null;
                        return result;
                    }
                    orderCondition.DeliveryDateStart = this.dtpDeliveryDateStart.Value.ToShortDateString() + " 00:00:00";
                    orderCondition.DeliveryDateEnd = this.dtpDeliveryDateEnd.Value.ToShortDateString() + " 23:59:59";
                }
                if (this.ckLailiaoUsed.Checked)
                {
                    if (this.ckLailiao.Checked)
                    {
                        orderCondition.IsLailiao = 1;
                    }
                    else
                    {
                        orderCondition.IsLailiao = 0;
                    }
                }
                if (this.ckQuliaoUsed.Checked)
                {
                    if (this.ckQuliao.Checked)
                    {
                        orderCondition.IsQuliao = 1;
                    }
                    else
                    {
                        orderCondition.IsQuliao = 0;
                    }
                }
                if (this.chkLockedOrderDataUsed.Checked)
                {
                    if (this.chkLockedOrderData.Checked)
                    {
                        orderCondition.Locked = 1;
                    }
                    else
                    {
                        orderCondition.Locked = 0;
                    }
                }
                if (this.chkCancelledOrderUsed.Checked)
                {
                    if (this.chkCancelledOrder.Checked)
                    {
                        orderCondition.Cancelled = 1;
                    }
                    else
                    {
                        orderCondition.Cancelled = 0;
                    }
                }
                if (this.txtOrderCode.Text.Trim() != "")
                {
                    orderCondition.OrderCode = this.txtOrderCode.Text.Trim();
                }
                if (this.txtRemark.Text.Trim() != "")
                {
                    if (this.txtRemark.Text.Trim().Length > 64)
                    {
                        MessageBox.Show("字符数量不能大于64", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtRemark.Focus();
                        result = null;
                        return result;
                    }
                    orderCondition.Remark = this.txtRemark.Text.Trim();
                }
                if (this.txtSalesperson.Text.Trim() != "")
                {
                    if (this.txtSalesperson.Text.Trim().Length > 16)
                    {
                        MessageBox.Show("字符数量不能大于16", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtSalesperson.Focus();
                        result = null;
                        return result;
                    }
                    orderCondition.Salesperson = this.txtSalesperson.Text.Trim();
                }
                result = orderCondition;
            }
            return result;
        }

        private void btnNosettlementOrder_Click(object sender, EventArgs e)
        {
            OrderCondition conditionForQuery = this.GetConditionForQuery();
            if (conditionForQuery != null)
            {
                DataTable dataTable = null;
                Orders orders = new Orders();
                try
                {
                    dataTable = orders.GetNosettlementOrdersByCondition(conditionForQuery);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！\r\n\r\n订单项信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message + "\r\n\r\n订单信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                orders.Dispose();
                if (dataTable != null)
                {
                    if (dataTable.Rows.Count > 0)
                    {
                        this.dgOrders.DataSource = dataTable;
                        this.dgOrders.AutoResizeColumns();
                    }
                    else
                    {
                        MessageBox.Show("根据你输入的条件，在系统中没有找到你要得信息", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.dgOrders.DataSource = null;
                    }
                    dataTable.Dispose();
                    if (this.dgOrders.Columns.Count > 0)
                    {
                        this.dgOrders.Columns["订单序号"].Visible = false;
                    }
                }
                this.txtOrderAmount.Text = "0.00";
            }
        }

        private void dgOrders_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.RowIndex < this.dgOrders.Rows.Count && e.ColumnIndex >= 0 && e.ColumnIndex < this.dgOrders.Columns.Count && this.dgOrders != null && this.dgOrders.Rows.Count > 0 && this.dgOrders.Columns[e.ColumnIndex].Name == "是否结算")
            {
                if ((bool)this.dgOrders.Rows[e.RowIndex].Cells[e.ColumnIndex].Value)
                {
                    this.dgOrders.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = false;
                }
                else
                {
                    this.dgOrders.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = true;
                }
                decimal d = 0m;
                for (int i = 0; i < this.dgOrders.Rows.Count; i++)
                {
                    if ((bool)this.dgOrders.Rows[i].Cells["是否结算"].Value)
                    {
                        d += Convert.ToDecimal(this.dgOrders.Rows[i].Cells["金额"].Value);
                    }
                }
                this.txtOrderAmount.Text = d.ToString("F2");
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.dgOrders.Rows.Count > 0)
            {
                bool flag = false;
                for (int i = 0; i < this.dgOrders.Rows.Count; i++)
                {
                    if ((bool)this.dgOrders.Rows[i].Cells["是否结算"].Value)
                    {
                        flag = true;
                        break;
                    }
                }
                if (!flag)
                {
                    MessageBox.Show("你没有选择任何数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    ArrayList arrayList = new ArrayList();
                    for (int i = 0; i < this.dgOrders.Rows.Count; i++)
                    {
                        if ((bool)this.dgOrders.Rows[i].Cells["是否结算"].Value)
                        {
                            arrayList.Add(this.dgOrders.Rows[i].Cells["订单号"].Value);
                        }
                    }
                    string[] aOrders = arrayList.ToArray(typeof(string)) as string[];
                    FormSelectOrders.s_OrdersAmountInfo = new OrdersAmountData(aOrders, Convert.ToDecimal(this.txtOrderAmount.Text));
                    base.Close();
                }
            }
            else
            {
                MessageBox.Show("表格中没有数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        public static OrdersAmountData ShowBox(int nCustomerID, string sCustomerName)
        {
            FormSelectOrders formSelectOrders = new FormSelectOrders(nCustomerID, sCustomerName);
            formSelectOrders.ShowDialog();
            return FormSelectOrders.s_OrdersAmountInfo;
        }
    }
}
